using System;
using System.Collections.Generic;
using System.Text;
using AZROLESLib;
using System.Threading;

namespace TBOAzMan
{
    class AzManSupport
    {
        IAzClientContext ctx;
        AzAuthorizationStore store;
        IAzApplication app;
        private string APP_NAME = "TBOAzMan";
        private string AM_STORE_PATH = @"msxml://e:\semestr10\TBO\aKsStore.xml";
        private string AM_TASK_PLACE_ORDER = "PlaceOrder";
        private string AM_TASK_COMMIT_ORDER = "CommitOrder";

        private enum AzOperations
        {
            op_PlaceOrder = 1,
            op_CommitOrder = 2
        }

        public AzManSupport()
        {
            store = new AzAuthorizationStoreClass();
            store.Initialize(0, AM_STORE_PATH, null);
            app = store.OpenApplication(APP_NAME, null);

            ctx = app.InitializeClientContextFromName(Thread.CurrentPrincipal.Identity.Name, null, null);
            //object roles = ctx.GetRoles("");
        }

        public void SwitchUser()
        {
            ctx = app.InitializeClientContextFromName(Thread.CurrentPrincipal.Identity.Name, null, null);
        }

        public bool CheckPlaceOrderPriv(int amount)
        {
            object[] operations = { AzOperations.op_PlaceOrder };
            object[] scopes = { "" };
            object[] varNames = { "amount", "roles" };
            object[] varValues = { amount, ctx.GetRoles("") };


            object[] results = (object[])ctx.AccessCheck(AM_TASK_PLACE_ORDER, scopes, operations, varNames, varValues, null, null, null);
            int res = Int32.Parse(results[0].ToString());
            if (res == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool CheckCommitOrderPriv()
        {
            object[] operations = { AzOperations.op_CommitOrder };
            object[] scopes = { "" };
            object[] varNames = { "roles" };
            object[] varValues = { ctx.GetRoles("") };


            object[] results = (object[])ctx.AccessCheck(AM_TASK_COMMIT_ORDER, scopes, operations, varNames, varValues, null, null, null);
            int res = Int32.Parse(results[0].ToString());
            if (res == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
